package com.jiaai.cloud.api;

import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.user.entity.TFollow;
import com.jiaai.cloud.user.service.ITFollowService;
import com.jiaai.cloud.utils.HttpHeaderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title FollowAPI
 * @since 2017/8/3
 */
@RestController()
@RequestMapping("/api/v1")
public class FollowAPI {
    @Autowired
    private ITFollowService followService;

    /**
     * 加载用户粉丝
     * @param userId
     * @return
     */
    @GetMapping("/fans/list/{userId}")
    public  Object findMyFans(@PathVariable("userId")String userId)throws ServiceException{
        return RestResultGenerator.genSuccessResult(followService.findFansByUserId(userId));
    }

    /**
     *加载用户关注列表
     * @param userId
     * @return
     */
    @GetMapping("/follow/list/{userId}")
    public  Object findMyFollow(@PathVariable("userId")String userId)throws ServiceException{
        return RestResultGenerator.genSuccessResult(followService.findFollowByUserId(userId));
    }

    /**
     *关注
     * @return
     */
    @GetMapping("/follow/{userId}")
    public  Object follow(@PathVariable("userId")String userId)throws ServiceException{
        TFollow follow=new TFollow();
        follow.setTargetId(userId);
        try {
            follow.setUserId(HttpHeaderUtils.getUserId());
        }catch (Exception e){
            throw new ServiceException("");
        }

        follow.setFollowTime(new Date());
        followService.insert(follow);
        return  RestResultGenerator.genSuccessResult();
    }
    @GetMapping("/follow/anchor")
    public  Object findMyFollowAnchor()throws  ServiceException{
        try {
            return RestResultGenerator.genSuccessResult(followService.findFollowByUserId(HttpHeaderUtils.getUserId()));
        } catch (Exception e) {
            e.printStackTrace();
            throw  new ServiceException("");
        }
    }
}
